package org.emongo.utils;

import java.util.regex.Pattern;

import com.mongodb.BasicDBList;
import com.mongodb.BasicDBObject;
import com.mongodb.QueryOperators;

public class QueryCondition {
	
	/**
	 * 拼装等于条件
	 * @param cond
	 * @param key
	 * @param val
	 * @return
	 */
	public static BasicDBObject keyEqual(BasicDBObject cond, String key, Object val){
		cond.put(key, new BasicDBObject(QueryOperator.EQ, val));
		
		return cond;
	}
	/**
	 * 拼装等于条件
	 * @param key
	 * @param val
	 * @return
	 */
	public static BasicDBObject keyEqual(String key, Object val){
		return keyEqual(new BasicDBObject(),key,val);
	}
	/**
	 * 拼装不等于条件
	 * @param cond
	 * @param key
	 * @param val
	 * @return
	 */
	public static BasicDBObject keyNotEqual(BasicDBObject cond, String key, Object val){
		cond.put(key, new BasicDBObject(QueryOperator.NE, val));
		
		return cond;
	}
	
	/**
	 * 拼装简单条件，等于、不等于、大于、小于、大于等于、小于等于
	 * @param cond
	 * @param key
	 * @param val
	 * @return
	 */
	public static BasicDBObject keySimpleCondition(BasicDBObject cond, String key, String operator, Object val){
		cond.put(key, new BasicDBObject(operator, val));
		return cond;
	}
	
	/**
	 * 拼装简单条件，等于、不等于、大于、小于、大于等于、小于等于
	 * @param key   QueryOperator类中选择操作符
	 * @param val
	 * @return
	 */
	public static BasicDBObject keySimpleCondition(String key, String operator, Object val){
		return keySimpleCondition(new BasicDBObject(),key,operator,val);
	}
	
	/**
	 * 拼装不等于条件
	 * @param key
	 * @param val
	 * @return
	 */
	public static BasicDBObject keyNotEqual(String key, Object val){
		return keyNotEqual(new BasicDBObject(),key,val);
	}
	
	/**
	 * 拼装between条件
	 * @param cond
	 * @param key
	 * @param start
	 * @param end
	 * @return
	 */
	public static BasicDBObject keyBetween(BasicDBObject cond, String key, Object start, Object end){
		
		BasicDBList condList = new BasicDBList();
		
		BasicDBObject cond1 = new BasicDBObject();
		cond1.put(key, new BasicDBObject(QueryOperator.GTE, start));
		condList.add(cond1);
		BasicDBObject cond2 = new BasicDBObject();
		cond2.put(key, new BasicDBObject(QueryOperator.LTE, end));
		condList.add(cond2);
		
		cond.put(QueryOperator.AND, condList);
		
		return cond;
	}
	/**
	 * 拼装between条件
	 * @param key
	 * @param start
	 * @param end
	 * @return
	 */
	public static BasicDBObject keyBetween(String key, Object start, Object end){
		return keyBetween(new BasicDBObject(), key, start, end);
	}
	
	/**
	 * 拼装in条件
	 * @param cond
	 * @param key
	 * @param val
	 * @return
	 */
	public static BasicDBObject keyIn(BasicDBObject cond, String key, Object... vals){
		
		BasicDBList valsList = new BasicDBList();
		for (Object v : vals) {
			valsList.add(v);
		}
		cond.put(key, new BasicDBObject(QueryOperator.IN, valsList));
		
		return cond;
	}
	/**
	 * 拼装in条件
	 * @param key
	 * @param val
	 * @return
	 */
	public static BasicDBObject keyIn(String key, Object... vals){
		
		return keyIn(new BasicDBObject(),key,vals);
	}
	
	/**
	 * 拼装not in条件
	 * @param cond
	 * @param key
	 * @param val
	 * @return
	 */
	public static BasicDBObject keyNotIn(BasicDBObject cond, String key, Object... vals){
		
		BasicDBList valsList = new BasicDBList();
		for (Object v : vals) {
			valsList.add(v);
		}
		cond.put(key, new BasicDBObject(QueryOperator.NIN, valsList));
		
		return cond;
	}
	/**
	 * 拼装not in条件
	 * @param key
	 * @param val
	 * @return
	 */
	public static BasicDBObject keyNotIn(String key, Object... vals){
		return keyNotIn(new BasicDBObject(),key,vals);
	}
	/**
	 * 拼装like条件  前后模糊匹配
	 * @param cond
	 * @param key
	 * @param val
	 * @return
	 */
	public static BasicDBObject keyLike(BasicDBObject cond, String key, String val){
		Pattern pattern = Pattern.compile("^.*" + val + ".*$",
				Pattern.CASE_INSENSITIVE);

		cond.put(key, pattern);
		
		return cond;
	}
	/**
	 * 拼装like条件  前后模糊匹配
	 * @param key
	 * @param val
	 * @return
	 */
	public static BasicDBObject keyLike(String key, String val){
		
		return keyLike(new BasicDBObject(),key,val);
	}
	
	/**
	 * 拼装多边形查询条件
	 * @param cond
	 * @param key
	 * @param polygonPoints 多边形点[经度,纬度]
	 * @return
	 */
	public static BasicDBObject keyPolygon(BasicDBObject cond, String key, double[][] polygonPoints){

		BasicDBList points = new BasicDBList();
		for (double[] td : polygonPoints) {
			points.add(td);
		}
		
		BasicDBObject polygon = new BasicDBObject();
		polygon.put(QueryOperators.POLYGON, points);
		
		BasicDBObject within = new BasicDBObject();
		within.put(QueryOperators.WITHIN, polygon);
		
		cond.put(key, within);
		
		return cond;
	}
	
	/**
	 * 拼装多边形查询条件
	 * @param key
	 * @param polygonPoints 多边形点[经度,纬度]
	 * @return
	 */
	public static BasicDBObject keyPolygon(String key, double[][] polygonPoints){
		return keyPolygon(new BasicDBObject(), key, polygonPoints);
	}
	/**
	 * 以一个点为圆点，以r为半径 查询该圆圈内的目标点信息
	 * @param cond
	 * @param circlePoints 圆点坐标
	 * @param r 半径
	 * @return
	 */
	public static BasicDBObject keyCenter(BasicDBObject cond,String key,double[] circlePoints,int r){
		BasicDBList params=new BasicDBList();
		params.add(circlePoints);
		
		params.add(r/111.0);
		
		BasicDBObject within=new BasicDBObject();
		within.put(QueryOperators.WITHIN, new BasicDBObject(QueryOperators.CENTER, params));
		cond.put(key, within);
		return cond;
	}
	public static BasicDBObject keyCenter(String key,double[] circlePoints,int r){
		return keyCenter(new BasicDBObject(),key,circlePoints,r);
	}

}
